#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <string.h>
#include <iostream>
#define PORT 5566
#define SIZE 30
using namespace std;

int main(){
	int socketfd;
	int ret;
	struct sockaddr_in address;
  struct hostent* host;
  struct netent* net;
	char buffer[SIZE];
  
  socketfd = socket(AF_INET,SOCK_STREAM,0);//创建一个套接字
  address.sin_family = AF_INET;//设置为因特网协议
  address.sin_port = htons(PORT);//设置端口号
  inet_pton(AF_INET,"127.0.0.1",&address.sin_addr.s_addr);//设置ip地址

  ret=connect(socketfd,(struct sockaddr*)&address,sizeof(address));//连接服务器
  if(ret == -1){
    printf("can't connect!\n");
    exit(0);
  }
  int n=0;
  memset(buffer,0,sizeof(buffer));
  sprintf(buffer,"log");
  n=send(socketfd,buffer,sizeof(buffer),MSG_WAITALL);
  if(n<0)perror("send log error\n");
  memset(buffer,0,sizeof(buffer));
	n=recv (socketfd,buffer,sizeof(buffer)/2,MSG_WAITALL);

  int size = atoi(buffer);
	cout<<"size "<<size<<endl;
  memset(buffer,0,sizeof(buffer));
	n=recv (socketfd,buffer,sizeof(buffer)/2,MSG_WAITALL);
	char filename[30];
  strcpy(filename,buffer);
	cout<<"filename "<<filename<<endl;
	int num_all=0;
  char* set_buffer_temp = new char[size+1000];
  memset(set_buffer_temp,0,size+1000);
  FILE *fp = fopen(filename,"wb+");
  do{
	n=recv (socketfd,set_buffer_temp,size,MSG_WAITALL);

      sleep(1);
	cout<<"recved n "<<n<<endl;
      if(n>0){
        num_all+=n;
        fwrite(set_buffer_temp,sizeof(char),n,fp);
      }
    }while(num_all<size);
    fflush(fp);
    fclose(fp);

memset(buffer,0,sizeof(buffer));
	n=recv (socketfd,buffer,sizeof(buffer)/2,MSG_WAITALL);

  size = atoi(buffer);
	cout<<"size "<<size<<endl;
  memset(buffer,0,sizeof(buffer));
	n=recv (socketfd,buffer,sizeof(buffer)/2,MSG_WAITALL);
  strcpy(filename,buffer);
	cout<<"filename "<<filename<<endl;
  char* net_buffer_temp = new char[size+1000];
  memset(net_buffer_temp,0,size+1000);
  fp = fopen(filename,"wb+");
  do{
	n=recv (socketfd,net_buffer_temp,size,MSG_WAITALL);

      sleep(1);
	cout<<"recved n "<<n<<endl;
      if(n>0){
        num_all+=n;
        fwrite(net_buffer_temp,sizeof(char),n,fp);
      }
    }while(num_all<size);
    fflush(fp);
    fclose(fp);

memset(buffer,0,sizeof(buffer));
	n=recv (socketfd,buffer,sizeof(buffer)/2,MSG_WAITALL);

   size = atoi(buffer);
	cout<<"size "<<size<<endl;
  memset(buffer,0,sizeof(buffer));
	n=recv (socketfd,buffer,sizeof(buffer)/2,MSG_WAITALL);
  strcpy(filename,buffer);
	cout<<"filename "<<filename<<endl;

  char* play_buffer_temp = new char[size+1000];
  memset(play_buffer_temp,0,size+1000);
  fp = fopen(filename,"wb+");
  do{
	n=recv (socketfd,play_buffer_temp,size,MSG_WAITALL);

      sleep(1);
	cout<<"recved n "<<n<<endl;
      if(n>0){
        num_all+=n;
        fwrite(play_buffer_temp,sizeof(char),n,fp);
      }
    }while(num_all<size);
    fflush(fp);
    fclose(fp);



	delete set_buffer_temp;
	delete net_buffer_temp;
	delete play_buffer_temp;
close(socketfd);
}






